import { createStore } from 'vuex'
export default createStore({
 state: {
    user: {
        id:'',
        name:'',
        token:'',
        level:'',
        score: ''
    },
    userList:[],
    total:0,
    modelList:[],
    modelTotal:0,
    curSelModel: 0
 },
 mutations: { 
    SETUSER(state, user) {
        state.user.id = user.id;
        state.user.name = user.name;
        state.user.token = user.token;
        state.user.level = user.level;
        state.user.score = user.score;
    },SETUSERLIST(state, {total, list}) {
        state.total = total;
        for (let i = 0; i < list.length; i++) {
            state.userList.push(list[i])
        }
    },ADDUSER(state, user) {
        state.total++
        state.userList.push(user)
    },SETMODELLIST(state, {total, module_list}) {
        state.modelTotal = total;
        for (let i = 0; i < module_list.length; i++) {
            state.modelList.push(module_list[i])
        }
    },ADDMODEL(state, data) {
        state.modelTotal++
        state.modelList.push(data)
    },SETCURMODEL(state, id) {
        state.curSelModel = id;
    },SETMODELBYID(state, md){
        let mlist = state.modelList;
        for (let i = 0; i < mlist.length; i++) {
            if (mlist[i].id == md.id) {
                mlist[i].icon = md.icon;
                mlist[i].name = md.name;
                mlist[i].desc = md.desc;
                mlist[i].level = md.level;
                mlist[i].score = md.score;
                break;
            }
        }
    }
 },
 actions: { 
    setUser({commit}, user) {
        commit('SETUSER', user)
    },setUserList({commit}, data) {
        commit("SETUSERLIST", data)
    },addUser({commit}, user) {
        commit("ADDUSER", user)
    },setModelList({commit}, data) {
        commit('SETMODELLIST', data)
    },addModel({commit}, data) {
        commit('ADDMODEL', data)
    },setCurSelModel({commit}, id) {
        commit('SETCURMODEL', id)
    },setModelByid({commit}, md) {
        commit('SETMODELBYID', md)
    }
 },
 modules: {
    
 } // 如果有多个模块，可以在此处定义
})